package com.bw.module_mine.ui.login.viewmodel

import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_common.base.state.RequestEvent
import com.bw.lib_storage.UserMMKV
import com.bw.module_mine.ui.login.SmsCode
import com.bw.module_mine.ui.login.repository.LoginRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

/**
 * @author zhy
 * @date 2025-09-22 16:34
 * @description
 */
@HiltViewModel
class LoginViewModel @Inject constructor(
    private val mRepository: LoginRepository
): BaseViewModel(){

    //保存当前正在显示的fragment
    private val _currentFragment = MutableStateFlow("")
    val currentFragment: StateFlow<String>
        get() = _currentFragment

    val phone:MutableStateFlow<String> = MutableStateFlow("")
    val password: MutableStateFlow<String> = MutableStateFlow("")
    val restPassword: MutableStateFlow<String> = MutableStateFlow("")
    val confirmPassword: MutableStateFlow<String> = MutableStateFlow("")
    //发送验证码结果
    private val requestSmsCode = MutableStateFlow<RequestEvent>(RequestEvent.Init)
    val requestSmsCode_ = requestSmsCode.asStateFlow()

    //登录请求事件
    private val requestLoginEvent = MutableSharedFlow<RequestEvent>()
    val requestLoginEvent_ get() = requestLoginEvent.asSharedFlow()


    //重置密码事件
    private val requestResetEvent = MutableSharedFlow<RequestEvent>()
    val requestResetEvent_ get() = requestResetEvent.asSharedFlow()
    //请求发送验证码的时间
    var sendSmsCodeTimeStamp: Long = 0L
    /**
     * 设置当前正在显示的fragment的名称
     */
    fun setShowingFragmentTag(tag: String) {
        _currentFragment.value = tag
    }

    /**
     * 清空手机号
     */
    fun clearPhone(){
        phone.value = ""
    }

    /**
     * 发送短信验证码，状态流
     */

    fun getSmsCode(type:String){
        execute(
            action = if (type == SmsCode.LOGIN.type)
                mRepository.sendLoginCode(phone.value)
            else if (type == SmsCode.RESET.type)
                mRepository.sendResetCode(phone.value)
            else
                mRepository.sendLoginCode(phone.value) ,// 绑定
            onStart = {
                requestSmsCode.value = RequestEvent.Loading
            },
            onError = {
                requestSmsCode.value = RequestEvent.Error(it)
            },
            onFinish = {
                requestSmsCode.value = RequestEvent.Success

            }

        )

    }
    //验证码登录
    fun loginBySmsCode(smsCode:String){
        execute(
            action = mRepository.login(grantType = "sms", phone = phone.value, smsCode = smsCode),
            onStart = {
                requestSmsCode.emit(RequestEvent.Loading)
            },
            onError = {
                requestSmsCode.emit(RequestEvent.Error(it))
            },
            onFinish = {
                requestLoginEvent.emit(RequestEvent.Success)
                //存储token
                UserMMKV.token = it.data.access_token
            }
        )

    }

    //账号密码登录
    fun loginByAccount(username: String, password: String){
        execute(
            action = mRepository.login(grantType = "password", phone = username, password = password),
            onStart = {
                requestSmsCode.emit(RequestEvent.Loading)
            },
            onError = {
                requestSmsCode.emit(RequestEvent.Error(it))
            },
            onFinish = {
                requestLoginEvent.emit(RequestEvent.Success)
                //存储token
                UserMMKV.token = it.data.access_token
            }
        )
    }

    //重置密码
    fun resetPwd(smsCode:String){
        execute(
            action = mRepository.resetPwd(grantType = "sms", phone = phone.value, smsCode = smsCode),
            onStart = {
                requestSmsCode.emit(RequestEvent.Loading)
            },
            onError = {
                requestSmsCode.emit(RequestEvent.Error(it))
            },
            onFinish = {
                requestResetEvent.emit(RequestEvent.Success)
            }
        )
    }

    //修改密码
    fun newPwd(){
        execute(
            action = mRepository.newPwd(grantType = "sms", password = restPassword.value, newPassword = confirmPassword.value),
            onStart = {
                requestSmsCode.emit(RequestEvent.Loading)
            },
            onError = {
                requestSmsCode.emit(RequestEvent.Error(it))
            },
            onFinish = {
                requestLoginEvent.emit(RequestEvent.Success)
            }
        )
    }
    fun clearRestPassword() {
        restPassword.value = ""

    }

    fun clearConfirmPassword() {
        confirmPassword.value = ""
    }

    fun clearPassword() {
        password.value = ""
    }
}